<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;

$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
$return['diy']=$this->plugin_setting('diy');
$return['basic']=$this->plugin_setting('basic');
$basic=$this->plugin_setting('basic');
$return['banner']=$basic['image'];
if($_RQ['op']=='main'){
	//查询所有未锁定、已审核的服务员工
	$worker_list=pdo_getall('jy_gohome_worker',array('uniacid'=>$_SESSION['uniacid'],'islock'=>'0','examine'=>'1'));
	if(empty($worker_list)){
		$worker_list = array(
			array('name'=>'技师小娜')
		);
	}
	if($worker_list){
		foreach($worker_list as &$item){
			$item['lev_name']=pdo_getcolumn('jy_gohome_lev',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['lev']),'lev_name');
		    //已支付，已确认，已完成订单数
			$num1=pdo_count('jy_gohome_order',array('uniacid'=>$_SESSION['uniacid'],'worker_id'=>$item['id'],'status'=>'1'));
			$num2=pdo_count('jy_gohome_order',array('uniacid'=>$_SESSION['uniacid'],'worker_id'=>$item['id'],'status'=>'2'));
			$num3=pdo_count('jy_gohome_order',array('uniacid'=>$_SESSION['uniacid'],'worker_id'=>$item['id'],'status'=>'4'));
			$item['num']=$num1+$num2+$num3;
		}
	}
	$return['pcd']=array(
		'province' =>trim($_RQ['province']),
		'city' =>trim($_RQ['city']),
		'district' =>trim($_RQ['district']),
	);
	$return['post']=array('dayid'=>0);
	$return['params']=array('order'=>'default');
	$return['worker_list']=$worker_list;
	$htmltitle = $return['basic']['title'];
	$share_desc = $return['basic']['share_desc'];
	$share_image = $return['basic']['share_image'];
	$return['sharedata']=array(
			'title'=>'技师列表',
			'desc'=>$share_desc?:'上门服务，技师入驻，经络排毒，活血化瘀！',
			'link'=>custom_url('jy_gohome/worker//&fromuser='.$_SESSION['uid'].'&current_plugin='.$_SESSION['current_plugin'],true),
			'imgUrl'=>ATTACHMENT_URL.$share_image,
	);
	exi($return);
}
if($_RQ['op']=='list'){

	exi($return);
}
if($_RQ['op']=='detail' || $_RQ['op']=='info'){
	if(!$_RQ['id']){
		exi('没有找到该员工，请重新选择！','error','jy_gohome/worker/main');
	}
	pdo_update('jy_gohome_worker',array('looks +='=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	//查询该员工具体信息
	$worker=pdo_get('jy_gohome_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$worker['lat_lng']=$worker['lat_lng']?:'{}';
	$worker['lat_lng']=json_decode($worker['lat_lng'],true);
	$worker['sales']=pdo_count('jy_gohome_order',array('uniacid'=>$_SESSION['uniacid'],'status'=>4,'worker_id'=>$v['id']),'id');
	//等级名称
	$lev_name=pdo_getcolumn('jy_gohome_lev',array('uniacid'=>$_SESSION['uniacid'],'id'=>$worker['lev_id']),'lev_name');
	//日期 今天，未来七天
	$data=array();
	$data['0']['riqi']=date("m-d",time("day")); 
	$data['1']['riqi']=date("m-d",strtotime("+1 day")); 
	$data['2']['riqi']=date("m-d",strtotime("+2 day")); 
	$data['3']['riqi']=date("m-d",strtotime("+3 day")); 
	$data['4']['riqi']=date("m-d",strtotime("+4 day")); 
	$data['5']['riqi']=date("m-d",strtotime("+5 day")); 
	$data['6']['riqi']=date("m-d",strtotime("+6 day")); 
	$weekarray=array("日","一","二","三","四","五","六"); //先定义一个数组
	$data['0']['xinq']= "星期".$weekarray[date("w")];
	$data['1']['xinq']= "星期".$weekarray[date("w",strtotime(date("Y-m-d",strtotime("+1 day"))))];
	$data['2']['xinq']= "星期".$weekarray[date("w",strtotime(date("Y-m-d",strtotime("+2 day"))))];
	$data['3']['xinq']= "星期".$weekarray[date("w",strtotime(date("Y-m-d",strtotime("+3 day"))))];
	$data['4']['xinq']= "星期".$weekarray[date("w",strtotime(date("Y-m-d",strtotime("+4 day"))))];
	$data['5']['xinq']= "星期".$weekarray[date("w",strtotime(date("Y-m-d",strtotime("+5 day"))))];
	$data['6']['xinq']= "星期".$weekarray[date("w",strtotime(date("Y-m-d",strtotime("+6 day"))))];
	//可服务项目
	$service=explode(";",$worker['service']);
	$service=array_filter($service);
	foreach($service as $k=>&$item){
		$item=pdo_get('jy_gohome_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item));	
		if(!$item){
			unset($service[$k]);
		}else{
			$item['package']=json_decode($item['package'],true);
		}
	}
	$return['worker']=$worker;
	$return['service']=$service;
	$return['province']=file_get_contents('core/resource/area.txt');
	$return['province']=unserialize($return['province']);
	$return['data']=$data;
	$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']),array('nickname','mobile'));
	$return['post']=array('dayid'=>0,'username'=>$member['nickname'],'mobile'=>$member['mobile']);
	$this->session('worker_id',$_RQ['id']);
	$return['sharedata']=array(
			'title'=>'技师-'.$worker['name'],
			'desc'=>$worker['remark']?:'上门服务，技师入驻，经络排毒，活血化瘀！',
			'link'=>custom_url('jy_gohome/worker/detail/id='.$worker['id'].'&fromuser='.$_SESSION['uid'].'&current_plugin='.$_SESSION['current_plugin'],true),
			'imgUrl'=>ATTACHMENT_URL.$worker['headimg'],
	);
	exi($return);
}
if($_RQ['op']=='notice'){
	//日期 今天，未来七天
	$data=array();
	$data['0']=date("Y-m-d",time("day")); 
	$data['1']=date("Y-m-d",strtotime("+1 day")); 
	$data['2']=date("Y-m-d",strtotime("+2 day")); 
	$data['3']=date("Y-m-d",strtotime("+3 day")); 
	$data['4']=date("Y-m-d",strtotime("+4 day")); 
	$data['5']=date("Y-m-d",strtotime("+5 day")); 
	$data['6']=date("Y-m-d",strtotime("+6 day"));
    $post=get_postdata();
	if($post){
		if($post['packageid']==''){
			exi('请选择服务套餐','error');
		}
		if($post['serve_id']==''){
			exi('请选择服务项目','error');
		}
		if(!$post['timeid']){
			exi('请选择服务时间','error');
		}
		if(!$post['username']){
			exi('请输入联系人姓名','error');
		}
		if(!$post['mobile']){
			exi('请输入联系人电话','error');
		}
		if(!preg_match("/^1[345678]{1}\d{9}$/",$post['mobile'])){  
	 
			exi('请输入正确的手机号码','error');
		} 
		if(!$post['address']){
			exi('请输入联系人地址','error');
		}
		if($post['fileds']){
			foreach ($post['fileds'] as $item) {
				if($item['ismust']==1 && empty($item['data'])){
					exi('请完善'.$item['title'].'内容后再提交','error');
				}
			}
		}
		//$package=ijson_encode(array('package_title'=>$post['package_title'],'package_price'=>$post['package_price']));
		//$time=ijson_encode(array('data'=>$post['data'],'start'=>$post['start'],'end'=>$post['end']));
		$package=pdo_getcolumn('jy_gohome_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$post['serve_id']),'package');
		$merchid=pdo_getcolumn('jy_gohome_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$this->session('worker_id')),'merchid');
		$package=json_decode($package,true);
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'user_id'=>$_SESSION['uid'],
			'merchid'=>$merchid,
			'serve_id'=>$post['serve_id'],
			'worker_id'=>$this->session('worker_id'),
			'package'=>ijson_encode($package[$post['packageid']-1]),
			'serve_time'=>$time,
			'timeid'=>$post['timeid']-1,
			'day'=>$data[$post['dayid']],
			'address'=>$post['address'],
			'pcd'=>ijson_encode($post['pcd']),
			'username'=>$post['username'],
			'mobile'=>$post['mobile'],
			'remark'=>$postdata['remark'],
			'ordersn'=>get_ordersn('jy_gohome_order','ordersn'),
			'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_gohome_order',$insert);
		$id=pdo_insertid();
		if(!$id){
			exi('创建订单失败！','error');
		}
		if($post['fileds']){
			foreach ($post['fileds'] as $item) {
			$data=array(
				'uniacid'=>$_SESSION['uniacid'],
				'user_id'=>$_SESSION['uid'],
				'serve_id'=>$post['serve_id'],
				'order_id'=>$id,
				'fileds_id'=>$item['id'],
				'createtime'=>TIMESTAMP,
			);
			if($item['type']==7){
				$data['data']=$item['data']['province'].$item['data']['city'].$item['data']['district'].$item['data']['xiangxi'];				
			}
			else{
				$data['data']=$item['data'];
			}
			pdo_insert('jy_gohome_data',$data);
			}
				
		}
	}
	
	exi('','','jy_gohome/book/main/id='.$id);
}